package com.gmrz.uap.jfinal.interceptor;

import com.gmrz.uap.common.UAPSSOHelper;
import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.UUID;

public class SessionManagerInterceptor implements Interceptor {


    public void intercept(Invocation inv) {


        HttpServletRequest request = inv.getController().getRequest();
        HttpServletResponse response = inv.getController().getResponse();

        Cookie cookie = UAPSSOHelper.findCookieByName(request, UAPSSOHelper.UAP_SESSION_ID);

        if (cookie == null) {
            cookie = new Cookie(UAPSSOHelper.UAP_SESSION_ID, UUID.randomUUID().toString().replaceAll("-", "").toUpperCase());
            cookie.setPath("/");
            UAPSSOHelper.addHttpOnlyCookie(response, cookie);
        }

        inv.invoke();
    }
}
